package com.sasu.university_data.controller.after;
import com.sasu.university_data.entity.AceTeacher;
import com.sasu.university_data.service.AceTeacherService;
import com.sasu.university_data.util.ResponseCode;
import com.sasu.university_data.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "王牌教师功能接口")
@RestController
@RequestMapping("/teacher")
public class AceTeacherController{
    @Resource
    private AceTeacherService aceTeacherService;

    @ApiOperation("分页查询数据")
    @GetMapping("/select")
    public ResponseData selectByPage( String schoolName, Integer page, Integer pageSize){

        List<AceTeacher> aceTeachers = aceTeacherService.selectByPage(schoolName, page, pageSize);
        if (aceTeachers==null){
            return new ResponseData(ResponseCode.NULL_DATA);
        }

        long count = aceTeacherService.count(schoolName);

        return new ResponseData(ResponseCode.SUCCESS,aceTeachers,count);
    }

    /**
     * 新增数据
     *
     * @param aceTeacher 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public ResponseData add( @RequestBody AceTeacher aceTeacher){

        if (!aceTeacherService.insert(aceTeacher)){

            return new ResponseData(ResponseCode.ADD_FAILED);
        }

        long count = aceTeacherService.count(null);

        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 更新数据
     *
     * @param aceTeacher 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public ResponseData edit(@RequestBody AceTeacher aceTeacher){

        if (!aceTeacherService.update(aceTeacher)){
            return new ResponseData(ResponseCode.UPDATE_FAILED);
        }

        long count = aceTeacherService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }

    /**
     * 通过主键删除数据
     *
     * @param atId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/delete")
    public ResponseData deleteById(Integer atId){

        if (!aceTeacherService.deleteById(atId)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = aceTeacherService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }


    /***
     * 通过id批量删除数据
     * @param ids
     * @return
     */
    @ApiOperation("通过id批量删除数据")
    @PostMapping("/lotDelete")
    public ResponseData deleteByIds(int[] ids){

        if (!aceTeacherService.deleteByIds(ids)){
            return new ResponseData(ResponseCode.DELETE_FAILED);
        }

        long count = aceTeacherService.count(null);
        return new ResponseData(ResponseCode.SUCCESS,"success",count);
    }
}
